﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using sauyou.Utils;

namespace sauyou.Admin
{
    public partial class Channel_Add : ManagePage
    {
        /// <summary>
        /// 编号
        /// </summary>
        private string mDataId
        {
            get
            {
                object obj = (object)ViewState["mdataId"];
                if (obj != null)
                    return (string)obj;
                else
                    return "";
            }
            set { ViewState["mdataId"] = value; }
        }

        private sauyou.DAL.Channel bll = new sauyou.DAL.Channel();
        private sauyou.Model.Channel model = new sauyou.Model.Channel();
        public int kindId; //栏目种类
        public int pId;    //栏目父ID
        public int classId;
        public string pTitle = "顶级类别";
        protected void Page_Load(object sender, EventArgs e)
        {

            mDataId = Request["dataId"];
            if (Request["opt"] != null)
            {
                btnUpdate.Visible = false;
                //取得栏目传参
                if (int.TryParse(Request.Params["kindId"], out kindId))
                {
                    if (int.TryParse(Request.Params["dataId"], out pId))
                    {
                        pTitle = bll.GetChannelTitle(pId);
                    }
                    else
                    {
                        pId = 0;
                    }
                    lblPid.Text = pId.ToString();
                    lblPname.Text = pTitle;
                    txtPageUrl.Text = ChannelUrl((Channel)kindId);

                }
                else
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要增加类别的种类不明确或参数不正确。", "back", "Error");
                }
            }
            else
            {
                btnSave.Visible = false;
                //取得栏目传参
                if (int.TryParse(Request.Params["kindId"], out kindId) && int.TryParse(Request.Params["dataId"], out classId))
                {
                    model = bll.GetModel(classId);
                    if (!Page.IsPostBack)
                    {
                        ShowInfo();
                    }
                }
                else
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改类别的种类不明确或参数不正确。", "back", "Error");
                }
            }

        }

        //绑定数据
        private void ShowInfo()
        {
            this.lblPid.Text = model.ParentId.ToString();
            if (model.ParentId > 0)
            {
                this.lblPname.Text = bll.GetChannelTitle(model.ParentId);
            }
            else
            {
                this.lblPname.Text = "顶级类别";
            }
            this.txtTitle.Text = model.Title;
            this.txtPageUrl.Text = model.PageUrl;
            this.txtSortId.Text = model.SortId.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int classId;
            int parentId = int.Parse(this.lblPid.Text.Trim());          //上一级目录
            int classLayer = 1;                                         //栏目深度
            string classList = "";


            model.Title = this.txtTitle.Text.Trim();
            model.ParentId = parentId;
            model.PageUrl = this.txtPageUrl.Text.Trim();
            model.SortId = int.Parse(this.txtSortId.Text.Trim());
            model.KindId = this.kindId;
            //添加栏目
            classId = bll.Add(model);
            //修改栏目的下属栏目ID列表
            if (parentId > 0)
            {
                DataSet ds = bll.GetChannelListByClassId(parentId);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    classList = dr["ClassList"].ToString().Trim() + classId + ",";
                    classLayer = Convert.ToInt32(dr["ClassLayer"]) + 1;
                }
            }
            else
            {
                classList = "," + classId + ",";
                classLayer = 1;
            }
            model.ClassList = classList;
            model.ClassLayer = classLayer;
            model.Id = classId;

            bll.Update(model);
            //保存日志
            SaveLogs("[栏目类别]添加类别：" + model.Title);
            JscriptPrint("增加栏目成功啦！", "Channel_List.aspx?kindId=" + kindId, "Success");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            model.Title = txtTitle.Text.Trim();
            model.PageUrl = txtPageUrl.Text.Trim();
            model.SortId = int.Parse(txtSortId.Text.Trim());
            //修改栏目
            bll.Update(model);
            //保存日志
            SaveLogs("[栏目类别]修改类别：" + model.Title);
            JscriptPrint("类别修改成功啦！", "List.aspx?kindId=" + kindId, "Success");
        }
    }
}
